package com.hc.comm.utils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CollectionUtils {

	
	
	/**
	 * 转为map 
	* @date 2016年1月25日 上午11:38:42
	* @param list
	* @param field
	* @return
	* @throws Exception
	* Map
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Map toMap(Collection list, String field) throws Exception{
		if (list != null) {
			Map result = new HashMap();
			for (Object object : list) {
				Field fd = BeanUtils.getClassField(field, object.getClass());
				fd.setAccessible(true);
				result.put(fd.get(object), object);
			}
			return result;
		}
		return null;
	}
	
	
	/**
	 * 获取list集合中javabean对于的field的值的集合
	 * 
	 * @param list
	 * @param field
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static List getBeanFieldValues(Collection list, String field) throws Exception {
		if (list != null) {
			List result = new ArrayList();
			for (Object object : list) {
				Field fd = BeanUtils.getClassField(field, object.getClass());
				fd.setAccessible(true);
				result.add(fd.get(object));
			}
			return result;
		}
		return null;
	}
	
}
